﻿using System.Configuration;
using ServiceStack.Redis;
using System.Collections.Generic;
using System;
using KeaoBase;

/// <summary>
/// RedisClientPool 的摘要说明
/// </summary>
public static class RedisClientPool
{
    private static PooledRedisClientManager m_manager;

    public static PooledRedisClientManager m_Manager
    {
        get
        {
            if (m_manager == null)
            {
                m_manager = new PooledRedisClientManager( new string[]{string.Format("{2}@{0}:{1}",ConfigurationManager.AppSettings["缓存地址"],ConfigurationManager.AppSettings["缓存端口"],ConfigurationManager.AppSettings["缓存密码"])});
                m_manager.ConnectTimeout = 1000;
            }
            return m_manager;
        }
    }

    public static IRedisClient GetRedisClient()
    {
        try
        {
            IRedisClient redis = m_Manager.GetClient();
            return redis;
        }
        catch(Exception err)
        {
            KeaoUtils.LogTxt(string.Format("获取缓存器异常。{0}", err.Message));
            return null;
        }
    }

    public static IRedisClient GetReadOnlyRedisClient()
    {
        try
        {
            IRedisClient redis = m_Manager.GetReadOnlyClient();
            DateTime serverTime = redis.GetServerTime();
            return redis;
        }
        catch(Exception err)
        {
            KeaoUtils.LogTxt(string.Format("获取只读缓存器异常。{0}", err.Message));
            return null;
        }
    }
}